package com.pa.authorization.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lww.springcloudapi.entity.TabEmployee;
import com.lww.springcloudapi.result.Result;
import com.pa.authorization.mapper.TabEmployeeLoginMapper;
import com.pa.authorization.service.TabEmployeeLoginService;
import com.pa.authorization.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 管理员登录 实现类
 * </p>
 *
 * @author SHEN
 * @since 2021-03-11
 */
@Service
public class TabEmployeeLoginServiceImpl extends ServiceImpl<TabEmployeeLoginMapper, TabEmployee> implements TabEmployeeLoginService {

    @Autowired
    TabEmployeeLoginMapper tabEmployeeLoginMapper;

    @Autowired
    RedisUtil redisUtil;

    @Override
    public TabEmployee findQueryByName(String userName) {
        TabEmployee tabEmployee = new TabEmployee();
        tabEmployee.setAccount(userName);
        TabEmployee sysEmployee = tabEmployeeLoginMapper.AdministratorLogin(tabEmployee);
        return sysEmployee;
    }

    @Override
    public List<String> findQueryName() {
        return tabEmployeeLoginMapper.findQueryName();
    }

    @Override
    public Result AdministratorLogin(TabEmployee tabEmployee) {
        TabEmployee sysEmployee = tabEmployeeLoginMapper.AdministratorLogin(tabEmployee);
        if (sysEmployee == null) {
            System.out.println("用户名错误");
            return Result.build(4001007, "用户名不存在");

        } else if (!sysEmployee.getPassword().equals(tabEmployee.getPassword())) {
            System.out.println("密码错误");
            return Result.build(4001007, "用户名密码不匹配");
        } else {
            System.out.println("登录成功");
            redisUtil.set("access-token", sysEmployee, 60, TimeUnit.MINUTES);
            return Result.ok(true);
        }
    }

    @Override
    public Result findQuery() {
        List<TabEmployee> tabEmployees = tabEmployeeLoginMapper.selectList(null);
        return Result.ok(tabEmployees);
    }


}
